/*************************************************
//  a layout manager for charts
//  write: xinjian zhang
//  data: 2011/5/10
*************************************************/
#pragma once

#include <vector>
#include "ChartProxy.h"
#include "AxisObjects.h"

using namespace std;

class CChartLayout
{
private:
    enum { HEAD_MARGIN = 18, FOOT_MARGIN = 0 };
private:
    int x;
    int y;
    int width;
    int height;
public:
    bool HaveChart() const
    {
        return chartList.size() > 0;
    }
public:
    void SetWidth(const int width)
    {
        this->width = width;
    }
    void SetHeight(const int height)
    {
        this->height = height;
    }
    void SetLeft(const int x)
    {
        this->x = x;
    }
    void SetTop(const int y)
    {
        this->y = y;
    }
    int Left() const
    {
        return x;
    }
    int Top() const
    {
        return y;
    }
    int Width() const
    {
        return width;
    }
    int Height() const
    {
        return height;
    }
public:
    vector<CChartProxy*> chartList;
public:
    CChartLayout(void)
    {
    }

    ~CChartLayout(void)
    {
        ClearChart();
    }

public:
    void AddChart(CChartProxy *chartProxy)
    {
        chartList.push_back( chartProxy );
    }

    void ClearChart()
    {
        vector<CChartProxy*>::iterator it;
        for ( it = chartList.begin(); it != chartList.end(); it++ )
        {
            delete *it;
        }
        chartList.clear();
    }

    void UpdateLayout()
    { 
        int cnt = chartList.size();
        if ( cnt == 0 ) return;
        int h = static_cast<int>( height / cnt );
        for (int i = 0; i < cnt; i++)
        {
            chartList[i]->SetOrigin( CPoint( x, h*(i+1) - CChartLayout::FOOT_MARGIN ) );
            chartList[i]->SetWidth( width );
            chartList[i]->SetHeight( h - CChartLayout::HEAD_MARGIN - CChartLayout::FOOT_MARGIN );
        }
    }

    void Reset()
    {
        for (int i = 0; i < chartList.size(); i++)
        {
            chartList[i]->Reset();
        }
    }

    void Paint(CDC *pdc)
    {       
        vector<CChartProxy*>::iterator it;
        for ( it = chartList.begin(); it != chartList.end(); it++ )
        {
            (*it)->Paint(pdc);
            
        }
    }

    void Zoom(const float factor)
    {
        vector<CChartProxy*>::iterator it;
        for ( it = chartList.begin(); it != chartList.end(); it++ )
        {
            (*it)->Zoom(factor);
        }
    }

    void SetCutline(CCutLine cutline)
    {
        vector<CChartProxy*>::iterator it;
        for ( it = chartList.begin(); it != chartList.end(); it++ )
        {
            (*it)->SetCutline(cutline);
        }
    }

    CCutLine GetCutline() const
    {
        if ( chartList.size() > 0)
            return chartList[0]->GetCutline();
        else
            return CCutLine(x, 0);
    }
};
